/**
 * 
 */
package tech.abing.spider.task.pageprocessor.amac;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.task.model.amac.PrivateDirectInvestMentFundStatEntity;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.UuIdPublic;

/**
 * 类说明
 * @author HUNING
 * @since 2018-1-4
 * 
 */
public class PrivateDirectInvestMentProcessor implements PageProcessor{
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		String json = page.getContent();
//		logger.info(json);
		
		JSONObject jsonObj = JSONObject.fromObject(json);
//		boolean hasPre = jsonObj.getBoolean("hasPre");
		String first= jsonObj.getString("first");
		
		//总页数
		if(first.equals("true")){
			long totalPages = jsonObj.getLong("totalPages");
			long totalCount = jsonObj.getLong("totalElements");
//			PrivateDirectInvestMentFundStatEntity fundStat = new PrivateDirectInvestMentFundStatEntity();
//			fundStat.setId(UuIdPublic.timeStrongID(5));
//			fundStat.setFundCount(String.valueOf(totalCount));
//			fundStat.setCreateTime(new Date());
//			objectContainer.add(fundStat);
			if(totalCount>1){
				
				PageRequest request = page.getRequest();
				for(int i = 0; i < totalPages; i++){
					String url = request.getUrl().replace("&page=0", "&page="+i+"")
					.replace("&nd=0.7476623745543449", TaskUtil.getRandomStrByDate());
					PageRequest req = context.createPageRequest(url, PrivateDirectInvestMentProcessor.class);
					req.setMethod(request.getMethod());
					req.setPostType(request.getPostType());
					
					req.setReqPayloadParams(request.getReqPayloadParams());
					req.setHeaders(request.getHeaders());
					queue.add(req);
				}
			}
		}
		
		//解析数据
		JSONArray result = jsonObj.getJSONArray("content");
		if(result !=null && result.size()>0){
			for(int i = 0;i<result.size();i++){
				JSONObject retObj = result.getJSONObject(i);
				String MPI_ID = retObj.getString("id");
				String rand = String.valueOf(Math.random());
				String url = "http://gs.amac.org.cn/amac-infodisc/api/pof/securities/"+MPI_ID+"?rand="+rand;
				PageRequest req = context.createPageRequest(url, PrivateDirectInvestMentDetailProcessor.class);
				req.setMethod(Method.GET);
//				req.setPostType(PostType.REQUEST_PAYLOAD);
//				req.setReqPayloadParams("{}");
//				req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
//				req.putHeader("Referer", "http://gs.amac.org.cn/amac-infodisc/res/pof/securities/index.html");
//				req.putHeader("X-Requested-With", "XMLHttpRequest");
//				req.putHeader("Host", "gs.amac.org.cn");
//				req.putHeader("Content-Type", "application/json");
//				req.putHeader("Origin", "http://gs.amac.org.cn");
//				req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
//				req.putHeader("Accept-Encoding", "gzip, deflate");
//				req.putHeader("Accept-Language", "zh-CN,zh;q=0.9");
//				req.putHeader("Connection", "keep-alive");
				
				
				queue.add(req);
			}
		}
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		
	}

}
